package com.chen.palmar.project.producer;

import android.os.Bundle;
import android.widget.TextView;

import com.chen.palmar.R;
import com.chen.palmar.base.BaseFragment;
import com.chen.palmar.common.datasource.DataCenter;
import com.chen.palmar.common.override.BannerImageLoader;
import com.chen.palmar.common.override.HttpSubscriber;
import com.chen.palmar.entity.ProducerHouseEntity;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import rx.Subscription;

/**
 * Created by chen on 2018/1/6.
 */

public class HouseInfoFragment extends BaseFragment {
    @Bind(R.id.banner_style)
    Banner bannerStyle;
    @Bind(R.id.banner_honor)
    Banner bannerHonor;
    @Bind(R.id.tv_producer_honor)
    TextView tvProducerHonor;
    @Bind(R.id.tv_producer_brand)
    TextView tvProducerBrand;
    @Bind(R.id.tv_producer_introduce)
    TextView tvProducerIntroduce;
    @Bind(R.id.tv_producer_ability)
    TextView tvProducerAbility;
    private List<Object> images = new ArrayList<>();
    private List<Object> imagesHonor = new ArrayList<>();

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_house;
    }

    @Override
    public void initData() {

        //设置指示器位置（当banner模式中有指示器时）
        bannerStyle.setIndicatorGravity(BannerConfig.CENTER);
        bannerStyle.setImageLoader(new BannerImageLoader());

        //设置指示器位置（当banner模式中有指示器时）
        bannerHonor.setIndicatorGravity(BannerConfig.CENTER);
        bannerHonor.setImageLoader(new BannerImageLoader());

        try {
//            getDataForWeb(getArguments().getString("info"));
        } catch (Exception e) {
            e.printStackTrace();
            showToast("服务器数据出错，请稍后重试");
        }
    }

    private void getDataForWeb(String info) throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        map.put("factoryId", info);
        Subscription subscribe = DataCenter.producerHouseInfo(map).subscribe(new HttpSubscriber<ProducerHouseEntity>(getContext(), null) {
            @Override
            public void onNext(ProducerHouseEntity listEntity) {
                if (listEntity.getData() == null) {
                    showToast("服务器返回数据为空，请联系管理员");
                    return;
                }

                images.clear();
                images.addAll(listEntity.getData().getStyleImg());
                bannerStyle.setImages(images);
                bannerStyle.start();

                imagesHonor.clear();
                imagesHonor.addAll(listEntity.getData().getHonorImg());
                bannerHonor.setImages(imagesHonor);
                bannerHonor.start();

                tvProducerBrand.setText(listEntity.getData().getPay_power());
                tvProducerAbility.setText("500以上");
                switch (listEntity.getData().getScale()) {
                    case 1:
                        tvProducerAbility.setText("1-50人");
                        break;
                    case 2:
                        tvProducerAbility.setText("50-100人");
                        break;
                    case 3:
                        tvProducerAbility.setText("100-500人");
                        break;
                    case 4:
                        tvProducerAbility.setText("500以上");
                        break;
                }
                tvProducerIntroduce.setText(listEntity.getData().getEstablish_at());

                tvProducerHonor.setText("  " + listEntity.getData().getHonorContent());
            }
        });
        subscription.add(subscribe);
    }
}
